<template>
  <Dialog v-model="superVisible" :title="title" class="keyword-dialog" width="600px">
    <div class="form-container">
      <el-form
        ref="superFormRef"
        :model="superForm"
        :rules="rules"
        label-width="100px"
        class="keyword-form"
      >
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:folder" class="section-icon" />
            <span>基本信息</span>
          </div>

          <el-form-item label="所属分支" prop="branch" class="form-item">
            <el-input
              v-model="superForm.branch"
              placeholder="请输入所属分支名称"
              class="form-input"
              clearable
            >
              <template #prefix>
                <Icon icon="ep:folder" class="input-icon" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="分支属性" prop="branchType" class="form-item">
            <el-select
              v-model="superForm.branchType"
              placeholder="请选择分支属性"
              class="form-select"
              clearable
            >
              <el-option label="肯定" :value="1">
                <div class="option-item">
                  <el-tag type="success" size="small">肯定</el-tag>
                  <span class="option-desc">表示积极、同意的回应</span>
                </div>
              </el-option>
              <el-option label="否定" :value="2">
                <div class="option-item">
                  <el-tag type="warning" size="small">否定</el-tag>
                  <span class="option-desc">表示消极、不同意的回应</span>
                </div>
              </el-option>
              <el-option label="拒绝" :value="3">
                <div class="option-item">
                  <el-tag type="danger" size="small">拒绝</el-tag>
                  <span class="option-desc">表示明确拒绝的回应</span>
                </div>
              </el-option>
              <el-option label="默认" :value="5">
                <div class="option-item">
                  <el-tag type="info" size="small">默认</el-tag>
                  <span class="option-desc">默认分支属性</span>
                </div>
              </el-option>
            </el-select>
          </el-form-item>
        </div>

        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:key" class="section-icon" />
            <span>关键词设置</span>
          </div>

          <el-form-item label="关键词" prop="keys" class="form-item">
            <el-input
              v-model="superForm.keys"
              type="textarea"
              placeholder="请输入关键词，多个关键词用逗号隔开\n例如：是的,好的,同意,确认"
              :rows="4"
              class="form-textarea"
              show-word-limit
              maxlength="500"
            ></el-input>
            <div class="input-tip">
              <Icon icon="ep:info-filled" class="tip-icon" />
              <span>多个关键词请用英文逗号（,）分隔</span>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button class="cancel-btn" @click="superVisible = false">
          <Icon icon="ep:close" class="btn-icon" />
          取消
        </el-button>
        <el-button
          type="primary"
          class="submit-btn"
          :disabled="fromDisable"
          :loading="fromDisable"
          @click="submit"
        >
          <Icon icon="ep:check" class="btn-icon" v-if="!fromDisable" />
          {{ fromDisable ? '提交中...' : '确定' }}
        </el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup>
import * as defaultIvrKnowApi from '@/api/call/manage'

const superVisible = ref(false)
const title = ref('知识库问题')
const superForm = reactive({
  id: '',
  branch: '',
  branchType: '',
  keys: '',
  type: 1,
  flowId: 0
})

const rules = reactive({
  branch: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
  branchType: [{ required: true, message: '分支属性不能为空', trigger: 'change' }]
})

const open = (type, idVal) => {
  superVisible.value = true
  if (idVal) {
    title.value = '编辑知识库问题'
    getDetail(idVal)
  } else {
    superForm.id = ''
    title.value = '创建知识库问题'
    resetForm()
  }
}

const getDetail = async (idVal) => {
  const { id, branch, keys, type, branchType } = await defaultIvrKnowApi.getIvrKeywords(idVal)
  Object.assign(superForm, {
    id,
    branch,
    keys,
    type,
    branchType
  })
}

const resetForm = () => {
  superForm.branch = ''
  superForm.keys = ''
  superForm.type = ''
  superForm.branchType = ''
}

const superFormRef = ref(false)
const fromDisable = ref(false)
const emit = defineEmits(['success'])

const submit = async () => {
  try {
    await superFormRef.value.validate()
    fromDisable.value = true
    if (superForm.id) {
      await defaultIvrKnowApi.updateIvrKeywords(superForm)
    } else {
      await defaultIvrKnowApi.createIvrKeywords(superForm)
    }
    ElMessage.success('操作成功')
    superVisible.value = false
    emit('success')
  } finally {
    fromDisable.value = false
  }
}

defineExpose({ open })
</script>

<style lang="scss" scoped>
// 对话框样式
.keyword-dialog {
  :deep(.el-dialog) {
    border-radius: 16px;
    overflow: hidden;

    .el-dialog__header {
      background: linear-gradient(135deg, #97a5e2 0%, #7db5db 100%);
      color: white;
      padding: 20px 24px;

      .el-dialog__title {
        color: white;
        font-size: 18px;
        font-weight: 600;
      }

      .el-dialog__headerbtn {
        .el-dialog__close {
          color: rgba(255, 255, 255, 0.8);
          font-size: 18px;

          &:hover {
            color: white;
          }
        }
      }
    }

    .el-dialog__body {
      padding: 0;
    }

    .el-dialog__footer {
      padding: 0;
      border-top: 1px solid #f1f5f9;
    }
  }
}

// 表单容器
.form-container {
  padding: 24px;
}

.keyword-form {
  .form-section {
    margin-bottom: 32px;

    &:last-child {
      margin-bottom: 0;
    }
  }
}

// 区域标题
.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #f1f5f9;

  .section-icon {
    color: #667eea;
    font-size: 18px;
  }

  span {
    font-size: 16px;
    font-weight: 600;
    color: #374151;
  }
}

// 表单项
.form-item {
  margin-bottom: 24px;

  :deep(.el-form-item__label) {
    font-weight: 500;
    color: #374151;
    line-height: 1.6;
  }

  :deep(.el-form-item__content) {
    line-height: normal;
  }
}

// 输入框样式
.form-input {
  :deep(.el-input__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focus {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }

  .input-icon {
    color: #9ca3af;
    font-size: 16px;
  }
}

// 选择框样式
.form-select {
  width: 100%;

  :deep(.el-select__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focused {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }
}

// 选项样式
.option-item {
  display: flex;
  align-items: center;
  gap: 12px;

  .option-desc {
    font-size: 12px;
    color: #6b7280;
  }
}

// 文本域样式
.form-textarea {
  :deep(.el-textarea__inner) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;
    font-family: inherit;
    resize: vertical;

    &:hover {
      border-color: #d1d5db;
    }

    &:focus {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }
}

// 输入提示
.input-tip {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  padding: 8px 12px;
  background: #f0f9ff;
  border: 1px solid #e0f2fe;
  border-radius: 6px;

  .tip-icon {
    color: #0ea5e9;
    font-size: 14px;
  }

  span {
    font-size: 13px;
    color: #0369a1;
  }
}

// 对话框底部
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  background: #f8fafc;
}

.cancel-btn {
  padding: 10px 20px;
  border-radius: 8px;
  border: 2px solid #e5e7eb;
  background: white;
  color: #6b7280;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    margin-right: 6px;
    font-size: 14px;
  }

  &:hover {
    border-color: #d1d5db;
    color: #374151;
  }
}

.submit-btn {
  padding: 10px 24px;
  border-radius: 8px;
  background: linear-gradient(135deg, #97a5e2 0%, #7db5db 100%);
  border: none;
  color: white;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    margin-right: 6px;
    font-size: 14px;
  }

  &:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
  }

  &:disabled {
    opacity: 0.7;
    cursor: not-allowed;
  }
}

// H5 响应式设计
@media (max-width: 768px) {
  .keyword-dialog {
    :deep(.el-dialog) {
      width: 95% !important;
      margin: 20px auto;

      .el-dialog__header {
        padding: 16px 20px;

        .el-dialog__title {
          font-size: 16px;
        }
      }
    }
  }

  .form-container {
    padding: 20px;
  }

  .keyword-form {
    :deep(.el-form-item__label) {
      font-size: 14px;
    }
  }

  .section-title {
    span {
      font-size: 15px;
    }
  }

  .dialog-footer {
    padding: 16px 20px;

    .cancel-btn,
    .submit-btn {
      padding: 12px 16px;
      font-size: 14px;
    }
  }
}

@media (max-width: 480px) {
  .keyword-dialog {
    :deep(.el-dialog) {
      width: 100% !important;
      margin: 0;
      height: 100vh;
      border-radius: 0;
    }
  }

  .form-container {
    padding: 16px;
  }

  .form-section {
    margin-bottom: 24px;
  }

  .form-item {
    margin-bottom: 20px;
  }

  .dialog-footer {
    flex-direction: column;
    gap: 8px;

    .cancel-btn,
    .submit-btn {
      width: 100%;
      justify-content: center;
    }
  }
}

// 深色模式支持
@media (prefers-color-scheme: dark) {
  .keyword-dialog {
    :deep(.el-dialog) {
      background: #1e293b;

      .el-dialog__body {
        background: #1e293b;
      }
    }
  }

  .form-container {
    background: #1e293b;
  }

  .section-title {
    border-color: #334155;

    span {
      color: #f1f5f9;
    }
  }

  .form-item {
    :deep(.el-form-item__label) {
      color: #e2e8f0;
    }
  }

  .input-tip {
    background: #1e3a8a;
    border-color: #1e40af;

    span {
      color: #93c5fd;
    }
  }

  .dialog-footer {
    background: #0f172a;
    border-color: #334155;
  }
}
</style>
